package com.igo.controller;


import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.igo.domain.datacollection.devicedata.*;
import com.igo.domain.datacollection.result.Result;
import com.igo.enums.devicedata.ProductTypeEnum;
import com.igo.mapper.devicedata.DeviceDataMapper;
import com.igo.mapper.devicedata.DeviceInfoMapper;
import com.igo.mapper.devicedata.FlatCarDataMapper;
import com.igo.mapper.devicedata.GantryCraneCaDataMapper;
import com.igo.service.devicedata.DeviceDataCalcdService;
import com.igo.util.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/deviceData")
public class DeviceDataController {

    @Resource
    private DeivceDataCalculateGantryCraneCa deivceDataCalculateGantryCraneCa;

    @Resource
    private DeviceDataCalculateFlatCar deviceDataCalculateFlatCar;

    @Resource
    private DeviceDataCalcdService deviceDataCalcdService;

    @Resource
    private DeviceCrashData deviceCrashData;

    @Resource
    private DeviceDataCalculateUtill deviceDataCalculateUtill;

    @Resource
    private DeviceInfoMapper deviceInfoMapper;

    @Resource
    private FlatCarDataMapper flatCarDataMapper;

    @Resource
    private GantryCraneCaDataMapper gantryCraneCaDataMapper;


    private Date startTime;

    private Date endTime;
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @GetMapping("/test01")
    @ResponseBody
    public String test01() throws InterruptedException {

        String strStartTime = "2024-10-01 00:00:00";
        Date start = DateUtil.parse(strStartTime);
        String strEndTime = "2025-07-31 23:59:59";
        Date end = DateUtil.parse(strEndTime);
        while(start.compareTo(end)<=0){
            endTime = DateUtil.offset(start, DateField.DAY_OF_MONTH, 1);
            endTime=DateUtil.offset(endTime, DateField.SECOND, -1);
            deviceCrashData.crashData(start, endTime);
            start=DateUtil.offset(endTime, DateField.SECOND, 1);
        }
        return "success";
    }

    @GetMapping("/test")
    @ResponseBody
    public String test() {
        String strStartTime = "2025-08-07 00:00:00";
        Date start = DateUtil.parse(strStartTime);
        String strEndTime = "2025-08-07 23:59:59";
        Date end = DateUtil.parse(strEndTime);
        while(start.compareTo(end)<=0){
            endTime = DateUtil.offset(start, DateField.DAY_OF_MONTH, 1);
            endTime=DateUtil.offset(endTime, DateField.SECOND, -1);
            deviceDataCalcdService.remove(new LambdaQueryWrapper<DeviceDataCalcd>()
                    .ge(DeviceDataCalcd::getCreateTime,start).le(DeviceDataCalcd::getCreateTime, endTime));

            deivceDataCalculateGantryCraneCa.calculateGantryCraneCaData(start,endTime);
            deviceDataCalculateFlatCar.calculateFlatCarData(start, endTime);
            start=DateUtil.offset(endTime, DateField.SECOND, 1);
        }
        System.out.println("执行 数采设备任务明细数据");
        return "success";
    }

    @GetMapping("/test02")
    @ResponseBody
    public String test02() {
        String strStartTime = "2024-10-01 00:00:00";
        Date start = DateUtil.parse(strStartTime);
        String strEndTime = "2025-01-14 23:59:59";
        Date end = DateUtil.parse(strEndTime);
        while(start.compareTo(end)<=0){
            endTime = DateUtil.offset(start, DateField.DAY_OF_MONTH, 1);
            endTime=DateUtil.offset(endTime, DateField.SECOND, -1);

            deviceDataCalculateUtill.crashData(start, endTime);
            start=DateUtil.offset(endTime, DateField.SECOND, 1);
        }
        System.out.println("执行 数采设备填充离线实时数据");
        return "success";
    }


    @GetMapping("/listByCalcd")
    @ResponseBody
    @ApiOperation("查看任务明细")
    public Result listByCalcd(@RequestParam String deviceNo, @RequestParam String calcdNo) {
        DeviceInfo deviceInfo = deviceInfoMapper.selectOne(new LambdaQueryWrapper<DeviceInfo>().eq(DeviceInfo::getDeviceNo, deviceNo));
        if (deviceInfo==null){
            return Result.success();
        }
        if (StringUtils.isNotBlank(calcdNo) && StringUtils.isNotBlank(deviceNo)){
            if (deviceInfo.getProductId().compareTo(ProductTypeEnum.FLATCAR.getValue())==0) {
                List<FlatCarData> flatCarData = flatCarDataMapper.selectList(new LambdaQueryWrapper<FlatCarData>()
                        .eq(FlatCarData::getDeviceNo,deviceNo).eq(FlatCarData::getCalcdNo, calcdNo).orderByAsc(FlatCarData::getDataTime));
                return Result.success(flatCarData);
            }else if (deviceInfo.getProductId().compareTo(ProductTypeEnum.GantryCrane.getValue())==0){
                List<GantryCraneCaData> gantryCraneCaData = gantryCraneCaDataMapper.selectList(new LambdaQueryWrapper<GantryCraneCaData>()
                        .eq(GantryCraneCaData::getDeviceNo, deviceNo)
                        .eq(GantryCraneCaData::getCalcdNo, calcdNo).orderByAsc(GantryCraneCaData::getDataTime));
                return Result.success(gantryCraneCaData);
            }else{
                return null;
            }
        }
        return Result.success();
    }

    @GetMapping("/detail")
    @ResponseBody
    public Result detail(@RequestParam String deviceNo , @RequestParam String dateTime) {
        DeviceInfo deviceInfo = deviceInfoMapper.selectOne(new LambdaQueryWrapper<DeviceInfo>().eq(DeviceInfo::getDeviceNo, deviceNo));
        Date date = DateUtil.parse(dateTime);
        //获得年的部分
        int year = DateUtil.year(date);
        //获得月份，从0开始计数
        int month = DateUtil.month(date)+1;
        //获得日
        int day = DateUtil.dayOfMonth(date);
        int hour = DateUtil.hour(date,true);
        int minute = DateUtil.minute(date);
        MinuteRangeCalculator minuteRangeCalculator = MinuteRangeCalculator.calculateMinuteRange(year, month, day, hour, minute);
        if (deviceInfo==null){
            return Result.success();
        }
        if (deviceInfo.getProductId().compareTo(ProductTypeEnum.FLATCAR.getValue())==0) {
            FlatCarData flatCarData = flatCarDataMapper.selectOne(new LambdaQueryWrapper<FlatCarData>()
                            .eq(FlatCarData::getDeviceNo,deviceNo)
                    .ge(FlatCarData::getCreateTime, minuteRangeCalculator.getStart())
                    .le(FlatCarData::getCreateTime, minuteRangeCalculator.getEnd()));
            return Result.success(flatCarData);
        }else if (deviceInfo.getProductId().compareTo(ProductTypeEnum.GantryCrane.getValue())==0){
            GantryCraneCaData gantryCraneCaData = gantryCraneCaDataMapper.selectOne(new LambdaQueryWrapper<GantryCraneCaData>()
                    .eq(GantryCraneCaData::getDeviceNo,deviceNo)
                    .ge(GantryCraneCaData::getCreateTime, minuteRangeCalculator.getStart())
                    .le(GantryCraneCaData::getCreateTime, minuteRangeCalculator.getEnd()));
            return Result.success(gantryCraneCaData);
        }else{
            return null;
        }
    }


    @GetMapping("/flatCarLocation/list")
    @ResponseBody
    public Result flatCarLocation() {
        List<String> flatCarList = deviceInfoMapper.selectList(new LambdaQueryWrapper<DeviceInfo>()
                .eq(DeviceInfo::getProductId, '2')).stream().map(DeviceInfo::getDeviceNo).collect(Collectors.toList());
        List<FlatCarData> flatCarDataList =new ArrayList<>();
        flatCarList.stream().forEach(flatCarDataNo->{
            FlatCarData flatCarData = flatCarDataMapper.selectOne(new LambdaQueryWrapper<FlatCarData>()
                    .eq(FlatCarData::getDeviceNo, flatCarDataNo).orderByDesc(FlatCarData::getDataTime).last("limit 1"));
            flatCarDataList.add(flatCarData);
        });
        return Result.success(flatCarDataList);
    }

}
